import neko.vm.Thread;
import swhx.Stream;
import neko.net.Socket;
class App {

	static var flash : swhx.Flash;
	static var cnx :  swhx.Connection;
	static var oldGetUrl;
	static var outSock : Socket;

	
	static function stringTest( string : String ) {
		return string;
	}

	static function main() {		
		swhx.Application.init(9); // Flash player 9 required.

		var width =780;
		var height = 445;
				// server		
		var server = new neko.net.RemotingServer();
		server.addObject("backend",App);

		// window
		var wnd = new swhx.Window("Flii rev 6",width,height);
		wnd.onMinimize = function () { 
			trace("window is minimizing"); 
			return true; // return false to block minimize;
		};		
		wnd.onMaximize = function () { 
			trace("window is maximizing"); 
			return true; // return false to block maximize;
		};		
		wnd.onRightClick = function() {
			trace("Supressing right-click menu by returning false on event");
			return false;
		};	
			
		wnd.resizable = true;		
		wnd.show(true);		

		// flash;
		flash = new swhx.Flash(wnd,server);
		flash.setAttribute("id","ui");
		flash.setAttribute("src","ui/FliiUI.swf");
		//flash.setAttribute("src","gamelets/FliiAppDemo.swf");
		flash.setAttribute("allowFullScreen","true");
		flash.onSourceLoaded = onSourceLoaded;
		oldGetUrl = flash.onGetURL;
		flash.onGetURL = myOnGetURL; 
		flash.start();
		
		// message loop:
		swhx.Application.loop();
		swhx.Application.cleanup();
		killWii();
	}

	static function onSourceLoaded() {
		trace("Main Flash content loading complete.");
		cnx = swhx.Connection.flashConnect(flash);
		Thread.create(fliiThread);
		
		Thread.create(socketThread);
		//fliiThread();
		//trace("hello(4,5) = "+cnx.hello.call([4,5]));
	}
	
    static function socketThread() {
		var s = new neko.net.Socket();
		s.bind(new neko.net.Host("localhost"),5000);
		s.listen(1);
		trace("Starting server...");
		outSock = s.accept();
		trace("connected");
		while( true ) {

		}
   }
    static function fliiThread() {
		trace("thread");
		

    	set_handler(mycb, fliiIR,fliiAC,fliiJS, fliiBUT);
		set_calibration(fliiACCAB, fliiJOYCAB);
		
		//mycb("Flii Started");
		beginWii();
    }
	
	static function hello( x : Int, y : Int ) {
	trace("hello");
		return "hello "+(x+y);
	}
	public static function fliiACCAB( type: Int, accX_zero: Int,  accY_zero: Int, accZ_zero : Int,accX_1g : Int,accY_1g : Int, accZ_1g : Int) {
		cnx.flii.fliiACCAB.call([type, accX_zero,accY_zero,accZ_zero,accX_1g,accY_1g, accZ_1g ]);
	}
	public static function fliiJOYCAB(type: Int, x_max: Int, x_min: Int, x_center: Int, y_max: Int, y_min: Int, y_center: Int) {
		cnx.flii.fliiJOYCAB.call([type,x_max, x_min, x_center, y_max, y_min, y_center]);
	}
	public static function fliiIR(x : Float,y : Float) {
		outSock.write("*ir:"+x+":"+y);
  
	}
	public static function fliiAC(type : Int, x : Int, y : Int, z: Int) {

		outSock.write("*ac:"+type+":"+x+":"+y+":"+z);
	
	}
	public static function fliiJS(type:Int, x : Int, y:Int ) {
		outSock.write("*js:"+type+":"+x+":"+y);
		//trace("joy");

	}
	public static function fliiBUT(type:Int, pressed:Bool ) {

		cnx.flii.fliiBUT.call([type,pressed]);		
	}

    public static function mycb(s:String) {
		//sync_call(myMethod);
		s =new String(s);
		//trace(a[0]);
    	cnx.flii.fliiData.call([s]);

    }
    public static function myMethod() {
		
    }
	static function myOnGetURL( s : Stream, url : String, postData : String) : Void {
		trace(url+":"+postData);
		//if (url.indexOf("?") == -1)
			oldGetUrl(s,url,postData);
		//else 
		//	oldGetUrl(s,"file:////Users/pete/flii/haxe/gamelets/LightSabre.swf",postData);
	}
	
	private static var sync_call = neko.Lib.load("swhx","sync_call",1);
    private static var beginWii = neko.Lib.load("flii","beginWii",0);
	private static var killWii = neko.Lib.load("flii","killWii",0);
    private static var set_handler = neko.Lib.load("flii","set_handler",5);
	private static var set_calibration = neko.Lib.load("flii","set_calibration",2);
}
